package com.wxp.demo05.button;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

/**
 * 2.1 复选框CheckBox
 */
public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_check_box);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("2.1 复选框CheckBox");

        CheckBox ck_system = this.findViewById(R.id.ck_system);
        CheckBox ck_custom = this.findViewById(R.id.ck_custom);
        //设置选择事件
        ck_system.setOnCheckedChangeListener(this);
        ck_custom.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String text = compoundButton.getText().toString();
        int lastIndexOf = text.lastIndexOf("\t\t您");
        if (lastIndexOf > 0) {
            text = text.substring(0, lastIndexOf);
        }
        compoundButton.setText(text + "\t\t您" + (b ? "选中" : "未选中") + "这个CheckBox");
    }
}